/**
 * This file is part of Megasquirt Logger for PalmOS.
 * Copyright (C) 2011 Andrew Beard
 *
 * Megasquirt Logger is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Megasquirt Logger is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Megasquirt Logger.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "LogManager.h"

/**
 * Constructor for the LogManager class.
 */
LogManager::LogManager()
{
    PopulateDatabaseNames();
}

/**
 * Destructor for the LogManager class.
 */
LogManager::~LogManager()
{
    if ( m_DatabaseNameList )
    {
        for ( UInt16 i = 0; i < m_nNumDatabaseNames; i++ )
        {
            delete[] m_DatabaseNameList[i];
            m_DatabaseNameList[i] = NULL;
        }
        delete[] m_DatabaseNameList;
        m_DatabaseNameList = NULL;
    }
}

/**
 * Returns the current list of database names.
 *
 * @return An array of pointers to the ASCII string with each database's name.
 */
Char** LogManager::GetDatabaseNames( void )
{
    return m_DatabaseNameList;
}

/**
 * Returns the number of database names held.
 *
 * @return The number of databases that hold MegaSquirt logs
 */
UInt16 LogManager::GetNumDatabaseNames( void ) const
{
    return m_nNumDatabaseNames;
}

/**
 * Checks if a given database contains a MegaSquirt log.
 *
 * @param[in]  databaseNunber The LocalID of the database to evaluate.
 * @param[out] databaseName   Pass in a buffer of size dmDBNameLength to get a
 *                              copy of the database name.  Pass in NULL
 *                              otherwise.
 * @return Returns true if the given database contains a MegaSquirt log.  False
 *           otherwise.
 */
bool LogManager::CheckDatabaseEligibility( LocalID databaseNumber,
                                           Char*   databaseName )
{
    UInt32 databaseType;
    UInt32 databaseCreator;
    Err    dbInfoError = errNone;

    dbInfoError = DmDatabaseInfo( 0,
                                  databaseNumber,
                                  databaseName,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  &databaseType,
                                  &databaseCreator );

    if ( (dbInfoError     == errNone) &&
         (databaseType    == 'Data' ) &&
         (databaseCreator == 'ABRD' ) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

/**
 * Populate the internal list of database names.
 */
void LogManager::PopulateDatabaseNames( void )
{
    UInt16  nNumDatabases           = 0;
    UInt16  nNumDatabaseNamesCopied = 0;
    LocalID databaseNumber;
    Char    currentName[ dmDBNameLength ];

    // Find out how many databases we have that are logs
    nNumDatabases       = DmNumDatabases( 0 );
    m_nNumDatabaseNames = 0;
    for ( UInt16 i = 0; i < nNumDatabases; i++ )
    {
        databaseNumber = DmGetDatabase( 0,
                                        i );
        if ( CheckDatabaseEligibility(databaseNumber, NULL) )
        {
            m_nNumDatabaseNames++;
        }
    }

    // Allocate memory for our list of names
    m_DatabaseNameList = new Char*[ m_nNumDatabaseNames ];
    for ( UInt16 i = 0; i < m_nNumDatabaseNames; i++ )
    {
        m_DatabaseNameList[i] = new Char[ dmDBNameLength ];
    }

    // Copy in the names
    nNumDatabaseNamesCopied = 0;
    for ( UInt16 i = 0; i < nNumDatabases; i++ )
    {
        databaseNumber = DmGetDatabase( 0,
                                        i );
        if ( CheckDatabaseEligibility(databaseNumber, currentName) )
        {
            StrCopy( m_DatabaseNameList[ nNumDatabaseNamesCopied++ ],
                     currentName );
        }
    }
}